package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService{

    @Lazy
    @Autowired
    private EmpService empService;
    /**
     * 根据id删除部门
     *
     * @param id
     */
    @Override
    public void removeDeptById(Integer id) {
        List<Emp> empList = empService.list(Wrappers.<Emp>lambdaQuery().eq(Emp::getDeptId, id));
        if (!CollectionUtils.isEmpty(empList)) {
            throw new RuntimeException("有员工不能删除该部门啊");
        }

        this.removeById(id);
    }

//    @Autowired
//    private DeptMapper deptMapper;
//
//
//    @Override
//    public List<Dept> findAll() {
//        return deptMapper.findAll();
//    }
//
//    @Override
//    public void deleteById(Integer id) {
//        deptMapper.deleteById(id);
//    }
//
//    @Override
//    public void save(Dept dept) {
//        // 补全基础属性
//        dept.setCreateTime(LocalDateTime.now());
//        dept.setUpdateTime(LocalDateTime.now());
//
//        // 调用Mapper层方法保存数据
//        deptMapper.save(dept);
//    }
//
//    @Override
//    public Dept getById(Integer id) {
//        Dept dept = deptMapper.getById(id);
//        return dept;
//    }
//
//    @Override
//    public void update(Dept dept) {
//        // 补全基础属性
//        dept.setUpdateTime(LocalDateTime.now());
//        deptMapper.update(dept);
//    }
}
